"""
Case Type   : openGauss 支持忽略PLSQL间的依赖进行创建
Case Name   : 存储过程的入参是自定义type，修改类型模式后重新调用
Create At   : 2023.11.15
Owner       : zhanghuan96
Description :
    1. 使用gs_guc reload方式设置参数为plpgsql_dependency
    2. 创建schema
    3. 创建复合类型
    4. 创建存储过程
    5. 调用存储过程
    6. 修改type所属模式
    7. 调用存储过程
    8. 清理环境
Expect      :
    1. 成功
    2. 成功
    3. 成功
    4. 成功
    5. 成功
    6. 成功
    7. 成功
    8.成功
History     :
    modified by zou_jialiang050 at 2024/01/8 适配最新代码
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class PlpgsqlDependency(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.common = Common()
        self.constant = Constant()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_node = Node('PrimaryDbUser')
        self.schema_name = "schema_guc0012"
        self.type_name = "type_guc0012"
        self.proc_name = "proc_guc0012"
        self.default_value = self.common.show_param('behavior_compat_options')

    def test_guc(self):
        text = '--step1:使用gs_guc reload方式设置参数为plpgsql_dependency;' \
               'expect:成功--'
        self.log.info(text)
        mod_msg = self.pri_sh.execute_gsguc('reload',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            "behavior_compat_options "
                                            "='plpgsql_dependency'")
        self.log.info(mod_msg)
        self.assertTrue(mod_msg)
        sql_cmd = "show behavior_compat_options;"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEqual('plpgsql_dependency',
                         sql_res.splitlines()[-2].strip(),
                         '执行失败:' + text)

        text = '--step2:创建schema;expect:成功--'
        self.log.info(text)
        sql_cmd = f'''drop schema if exists {self.schema_name} cascade;
        drop schema if exists {self.schema_name}_01 cascade;
        create schema {self.schema_name};
        create schema {self.schema_name}_01;'''
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEqual(2, sql_res.count('CREATE SCHEMA'), '执行失败:' + text)

        text = '--step3:创建复合类型;expect:成功--'
        self.log.info(text)
        sql_cmd = f'''drop type if exists {self.schema_name}.{self.type_name};
        create type {self.schema_name}.{self.type_name} as (
            id integer,
            name varchar,
            addr text
        );'''
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('CREATE TYPE', sql_res, '查询失败')

        text = '--step4:创建存储过程;expect:成功--'
        self.log.info(text)
        sql_cmd = f'''create or replace procedure \
        {self.schema_name}.{self.proc_name}\
        (a {self.schema_name}.{self.type_name})
        is
        begin
            RAISE INFO 'call a: %', a;
        end;
        '''
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('CREATE PROCEDURE', sql_res, '查询失败')

        text = '--step5:调用存储过程;expect:成功--'
        self.log.info(text)
        sql_cmd = f"call {self.schema_name}.{self.proc_name}" \
                  f"((1,'zhang','shanghai'));"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('INFO:  call a: (1,zhang,shanghai)', sql_res, '查询失败')

        text = '--step6:修改type所属模式;expect:成功--'
        self.log.info(text)
        sql_cmd = f"alter type {self.schema_name}.{self.type_name} set " \
                  f"schema {self.schema_name}_01;"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        err_msg = f"ERROR:  The set schema operator of {self.type_name} " \
                  f"is not allowed"
        self.assertIn(err_msg, sql_res, '查询失败')

        text = '--step7:调用存储过程;expect:成功--'
        self.log.info(text)
        sql_cmd = f"call {self.schema_name}.{self.proc_name}" \
                  f"((1,'zhang','shanghai'));"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('INFO:  call a: (1,zhang,shanghai)', sql_res, '查询失败')

    def tearDown(self):
        text = '----step8:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop procedure {self.schema_name}.{self.proc_name};" \
                  f"drop type if exists {self.schema_name}.{self.type_name};" \
                  f"drop schema if exists {self.schema_name} cascade;" \
                  f"drop schema if exists {self.schema_name}_01 cascade;"
        self.log.info(sql_cmd)
        sql_res = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        mod_msg = self.pri_sh.execute_gsguc('reload',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            f"behavior_compat_options "
                                            f"='{self.default_value}'")
        self.log.info(mod_msg)
        self.assertTrue(mod_msg)
        self.assertIn('DROP PROCEDURE', sql_res, '删除失败')
        self.assertIn('DROP TYPE', sql_res, '删除失败')
        self.assertIn('DROP SCHEMA', sql_res, '删除失败')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
